package com.yuchen.advsdk.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {
    public static void clearFiles(String parentFilePath){
        File parentFile = new File(parentFilePath);
        if (parentFile.isDirectory()){
            File[] files = parentFile.listFiles();
            for (File f:files){
                if (f.isFile() && f.exists()){
                    f.delete();
                }
            }
        }
    }

    public static String getStringMD5(String s){
        byte[] buffer = s.getBytes();
        MessageDigest digest = null;
        try{
            digest = MessageDigest.getInstance("MD5");
            digest.update(buffer, 0, buffer.length);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder("");
        byte[] src = digest.digest();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public static String getFileMD5(File file) {
        if (!file.isFile()) {
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        StringBuilder stringBuilder = new StringBuilder("");
        byte[] src = digest.digest();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public static String readFile(String path){
        return readFile(path,1024);
    }

    public static String readFile(String path, int bufferSize){
        StringBuilder sb =  new StringBuilder();
        InputStream fis = null;
        try {
            fis = new FileInputStream(path);
            byte[] buffer = new byte[bufferSize];

            int len = 0;
            while ((len = fis.read(buffer))!=-1){
                sb.append(new String(buffer,0,len));
            }

        } catch (IOException e) {
            e.printStackTrace();
//            MyLog.JUN_KANG.d(" ***ERROR*** read file: " + e.getMessage());
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    fis = null;
                }
            }
        }
        return sb.toString();
    }

    public static List<String> readLine(String fileName) {
        List<String> list = new ArrayList<>();
        try {
            File file = new File(fileName);
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String str;

            while ((str = br.readLine())!=null) {
                list.add(str);
            }
            br.close();
            fr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
